package com.yashi.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yashi.dao.TbDepartmentMapper;
import com.yashi.entity.TbDepartment;
import com.yashi.service.DepartmentService;
import com.yashi.utils.YSUtils;

@Service
public class DepartmentServiceImpl implements DepartmentService{

	@Resource
	private TbDepartmentMapper deptDao;
	
	@Override
	public Integer insertDept(TbDepartment department) throws Exception {
		department.setId(YSUtils.getUUID());
		department.setCreateDate(YSUtils.getCurrentDate());
		department.setDelFlag("0");
		int deptCount = deptDao.insertSelective(department);
		if(deptCount>0){
			return deptCount;
		}else{
			throw new Exception("添加部门失败");
		}
	}

	@Override
	public PageInfo<TbDepartment> findDeptList1(Integer pageNum) throws Exception {
		PageHelper.startPage(pageNum, 5);
		List<TbDepartment> findDeptList = deptDao.findDeptList1();
		PageInfo<TbDepartment> page = new PageInfo<TbDepartment>(findDeptList);
		return page;
	}

	@Override
	public Integer updateDept(TbDepartment department) throws Exception {
		//设置修改时间
		department.setUpdateDate(YSUtils.getCurrentDate());
		int updateCount = deptDao.updateByPrimaryKeySelective(department);
		if(updateCount>0){
			return updateCount;
		}else{
			throw new Exception("更新失败");
		}
	}

	@Override
	public Integer deleteDept(TbDepartment department) throws Exception {
		department.setDelFlag("1");
		Integer deleteDept = deptDao.deleteDept(department);
		if(deleteDept>0){
			return deleteDept;
		}else{
			throw new Exception("删除失败");
		}
	}

	@Override
	public TbDepartment findDeptById(String deptId) throws Exception {
		TbDepartment dept = deptDao.selectByPrimaryKey(deptId);
		if(dept!=null){
			return dept;
		}else{
			throw new Exception("查找的部门不存在");
		}
	}

	/**
	 * 模糊查询
	 * @throws Exception 
	 */
	@Override
	public PageInfo<TbDepartment> selectDeptByName(String deptName,Integer pageNum) throws Exception {
		PageHelper.startPage(pageNum, 5);
		List<TbDepartment> findDeptList = deptDao.selectDeptByName(deptName);
		PageInfo<TbDepartment> page = new PageInfo<TbDepartment>(findDeptList);
		return page;
	}

	/**
	 * app端查询所有部门
	 */
	@Override
	public List<TbDepartment> findDeptList() {
		List<TbDepartment> findDeptList = deptDao.findDeptList();
		return findDeptList;
	}

	/**
	 * app端部门模糊查询
	 */
	@Override
	public List<TbDepartment> selectDeptByName1(TbDepartment tbDept) {
		List<TbDepartment> selectDeptByName = deptDao.selectDeptByName(tbDept.getDepartmentname());
		return selectDeptByName;
	}

	@Override
	public List<TbDepartment> findDeptListToAdd() {
		return deptDao.findDeptList();
	}

}
